package fr.insta.reverse.uml.bean;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "user",
		catalog = "reverseUml",
		uniqueConstraints = @UniqueConstraint(columnNames = { "usr_mail" }))
public class User implements java.io.Serializable
{

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "usr_id", unique = true, nullable = false)
	private int id;

	@Column(name = "usr_nom", length = 50)
	private String nom = "";

	@Column(name = "usr_prenom", length = 30)
	private String prenom = "";

	@Column(name = "usr_mail", length = 50)
	private String mail = "";

	@Column(name = "usr_pwd", length = 40)
	private String password;

	@Transient
	private String passwordConfirm;

	//	@ManyToMany(cascade = CascadeType.ALL)
	//	@JoinTable(
	//			name = "autorisation",
	//			joinColumns = @JoinColumn(name = "auto_usr"),
	//			inverseJoinColumns = @JoinColumn(name = "auto_prj"),
	//			uniqueConstraints = @UniqueConstraint(columnNames = { "auto_usr", "auto_prj" }))
	//	private List<Projet> projetsAutorises;

	@OneToMany(mappedBy = "prop", cascade = CascadeType.ALL)
	private List<Projet> projets;

	public User()
	{
	}

	public User(String mail, String password)
	{
		this.mail = mail;
		this.password = password;
	}

	public User(String nom, String prenom, String mail, String password)
	{
		this.nom = nom;
		this.prenom = prenom;
		this.mail = mail;
		this.password = password;
	}

	public User(int id, String nom, String prenom, String mail, String password)
	{
		this.id = id;
		this.nom = nom;
		this.prenom = prenom;
		this.mail = mail;
		this.password = password;
	}

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public String getNom()
	{
		return nom;
	}

	public void setNom(String nom)
	{
		this.nom = nom;
	}

	public String getPrenom()
	{
		return prenom;
	}

	public void setPrenom(String prenom)
	{
		this.prenom = prenom;
	}

	public String getMail()
	{
		return mail;
	}

	public void setMail(String mail)
	{
		this.mail = mail;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public void addProjet(Projet p)
	{
		if (this.projets == null)
		{
			this.projets = new ArrayList<Projet>();
		}
		this.projets.add(p);
	}

	public void removeProjet(Projet p)
	{
		this.projets.remove(p);
	}

	public List<Projet> getProjets()
	{
		return projets;
	}

	public void setProjets(List<Projet> projets)
	{
		this.projets = projets;
	}

	public String getPasswordConfirm()
	{
		return passwordConfirm;
	}

	public void setPasswordConfirm(String password)
	{
		this.passwordConfirm = password;
	}

	@Override
	public String toString()
	{
		return id + ", " + nom + ", " + prenom + ", " + mail + ", " + password;
	}
}
